import { SUCCESS_CODE } from '@/core/constants/coreConstants'
import { ResultData } from '@/components/axios/interface'
import Oauth2RevokeResponse from '@/core/oauth2/types/model/response/Oauth2RevokeResponse'
import Oauth2IntrospectResponse from '@/core/oauth2/types/model/response/Oauth2IntrospectResponse'
import Oauth2IntrospectUserInfoResponse from '@/core/oauth2/types/model/response/Oauth2IntrospectUserInfoResponse'
import { MockMethod } from 'vite-plugin-mock'
import mockUtils from '../utils/mockUtils'
import oauth2ApiConfig from '@/core/oauth2/apiconfig/oauth2ApiConfig'

const timeout = 50

const List: {
	username: string
	password: string
	role: string
	roleId: string
	permissions: string | string[]
}[] = [
	{
		username: 'admin',
		password: 'admin',
		role: 'admin',
		roleId: '1',
		permissions: ['*.*.*'],
	},
	{
		username: 'test',
		password: 'test',
		role: 'test',
		roleId: '2',
		permissions: ['example:dialog:create', 'example:dialog:delete'],
	},
]

const mockList = [
	// 登录接口
	{
		url: oauth2ApiConfig.getTokenByPasswordGrantType.mockUri,
		method: oauth2ApiConfig.getTokenByPasswordGrantType.method,
		timeout,
		response: ({ body }: any) => {
			const data = body
			let hasUser = false
			for (const user of List) {
				console.log(user.username === data.username && user.password === data.password, user)
				if (user.username === data.username && user.password === data.password) {
					hasUser = true
					return {
						access_token: 'e04b892e-9b79-4785-9f8d-d5eaac399b7e',
						refresh_token: '8e126617-d68a-4c3f-8a71-1a96901ad90e',
						scope: 'all openid profile internal_client userinfo',
						token_type: 'Bearer',
						expires_in: 1800,
					}
				}
			}

			return {
				error: 1,
				error_description: '账号或密码错误',
			}
		},
	},
	// client_credentials 登录接口
	{
		url: oauth2ApiConfig.getTokenByClientGrantType.mockUri,
		method: oauth2ApiConfig.getTokenByClientGrantType.method,
		timeout,
		response: ({ body }: any) => {
			return {
				access_token: 'e04b892e-9b79-4785-9f8d-d5eaac399b7e',
				refresh_token: '8e126617-d68a-4c3f-8a71-1a96901ad90e',
				scope: 'all openid profile internal_client userinfo',
				token_type: 'Bearer',
				expires_in: 1800,
			}
		},
	},
	// 解析token接口
	{
		url: oauth2ApiConfig.oauth2IntrospectApi.mockUri,
		method: 'post',
		timeout,
		response: (res: any) => {
			console.log(res)
			return {
				active: true,
				sub: 'accountId',
				aud: ['defaultClientId'],
				nbf: 1709885594,
				scope: 'all openid profile internal_client userinfo',
				iss: 'http://localhost:8080',
				attributes: { nickName: 'nickName' },
				exp: 1709887394,
				iat: 1709885594,
				jti: '5c84c779-6a0a-4678-9ded-e49537cfe82f',
				client_id: 'defaultClientId',
				token_type: 'Bearer',
				userinfo: {
					id: 'accountId',
					attributes: { getUserInfo: 'accountId' },
					authorities: ['SCOPE_userinfo', 'read', 'SCOPE_internal_client', 'write'],
				} as Oauth2IntrospectUserInfoResponse,
			} as Oauth2IntrospectResponse
		},
	},
	// 退出接口
	{
		url: oauth2ApiConfig.oauth2RevokeApi.mockUri,
		method: 'post',
		timeout,
		response: () => {
			return {
				code: SUCCESS_CODE,
				data: { active: true } as Oauth2RevokeResponse,
			} as ResultData<Oauth2RevokeResponse>
		},
	},
] as MockMethod[]

mockUtils.addPriorityMockList(1, ...mockList)
